package com.ccgk.logistics.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ccgk.logistics.domain.OrderFee;
import com.ccgk.logistics.domain.OrderFeeExample;
import com.ccgk.logistics.persistence.OrderFeeMapper;
import com.ccgk.logistics.service.BusinessException;
import com.ccgk.logistics.service.CurrencyService;
import com.ccgk.logistics.service.FeeService;
import com.ccgk.logistics.service.OrderFeeService;
import com.ccgk.logistics.service.OrderService;

@Service
public class OrderFeeServiceImpl implements OrderFeeService{

	@Autowired
	private OrderFeeMapper orderFeeMapper;
	@Autowired
	private CurrencyService currencyService;
	@Autowired
	private FeeService feeService;
	@Autowired
	private OrderService orderService;
	
	@Override
	public Integer add(OrderFee orderFee) throws BusinessException{
		Assert.notNull(orderFee.getType());
		Assert.notNull(currencyService.getById(orderFee.getCurrency().getId()));
		Assert.notNull(feeService.getById(orderFee.getFee().getId()));
		Assert.notNull(orderService.getById(orderFee.getOrder().getId()));
		
		OrderFeeExample example = new OrderFeeExample();
		example.createCriteria().andOrderEqual(orderFee.getOrder().getId()).andFeeIdEqualTo(orderFee.getFee().getId()).andTypeEqualTo(orderFee.getType());
		if(orderFeeMapper.selectByExample(example).size() > 0){
			throw new BusinessException("费用重复");
		}
		
		orderFee.setTotal(orderFee.getPrice().multiply(orderFee.getUnits()));
		
		orderFeeMapper.insert(orderFee);
		return orderFee.getId();
	}

	@Override
	public void batchDelete(Integer[] idIntArray) {
		for(Integer id : idIntArray)
			delete(id);
		
	}

	@Override
	public Integer delete(int id) {
		Assert.notNull(id);
		orderFeeMapper.deleteByPrimaryKey(id);
		return id;
	}

	@Override
	public OrderFee getById(int id) {
		Assert.notNull(id);
		return orderFeeMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(OrderFee orderFee) {
		if(orderFee.getFee() != null && orderFee.getFee().getId() != null)
			Assert.notNull(feeService.getById(orderFee.getFee().getId()));
		if(orderFee.getCurrency() != null && orderFee.getCurrency().getId() != null)
			Assert.notNull(currencyService.getById(orderFee.getCurrency().getId()));
		OrderFee _orderFee = getById(orderFee.getId());
		BeanUtils.copyProperties(orderFee, _orderFee, new String[]{"order","type","fee"});
		_orderFee.setTotal(_orderFee.getPrice().multiply(_orderFee.getUnits()));
		orderFeeMapper.updateByPrimaryKey(_orderFee);
		
	}

	@Override
	public List<OrderFee> getExternalFeeList(Integer orderId) {
		return listFeeList(orderId, 2);
	}
	
	private List<OrderFee> listFeeList(Integer orderId,Integer type){
		OrderFeeExample example = new OrderFeeExample();
		example.createCriteria().andTypeEqualTo(type).andOrderEqual(orderId);
		return orderFeeMapper.selectByExample(example);
	}

	@Override
	public List<OrderFee> getInternalFeeList(Integer orderId) {
		return listFeeList(orderId,1);
	}

}
